import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import fromItem from '@/components/from-item/from-item.vue'
import lsjUpload from '@/uni_modules/lsj-upload/components/lsj-upload/lsj-upload.vue';
import {
	log
} from 'util'
export default {
	props: {
		mustIcon: {
			type: String,
			default: require('@/static/Scope.png'),
		},
		unIcon: {
			type: String,
			default: require('@/static/unscope.png'),
		}
	},
	data() {
		return {
			Inv: 0,
			// 详情
			totalData: {
				fId: "", // 申请单fid //为空判断为新增
				fInspectionnote: "", //报验申请
				fDirectoruserid: "", //技术负责人id
				fDirectorusername: "", //技术负责人
				fInspectiondates: "", //报验时间日期
				fJluserid: "", //提请专监审核人id
				fJlusername: "", //提请专监审核人

				fJladuitnote: "", //专监审核意见
				fJladuituserid: "", //专业监理工程师id
				fJladuitusername: "", //专业监理工程师
				fJladuitdates: "" //审核日期
			}, //详情数据
			Fdata: {}, //上个页面传过来的参数
			JlusernameList: [], //监理用户列表初始值
			//流程
			must0: "must", //必填
			must1: "must",
			readonly0: false, //是否只读
			readonly1: false,
			closeNum: -1, //闭合处置结果
			FirstNum: -1,
			handle0: ["同意", "不同意"],
			flagNum: -1, //日期选择是否可选
			clickN: 1, //按钮是否可用
			// 流程
			// 详情

			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式

			userInfo: null, //当前登录用户
			timing: null, //当前年月日
		};
	},



	onLoad(options) {
		console.log("options",options);
		var that = this;
		uni.getStorage({
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data);
				that.getJlusername(that.userInfo.jlCompanyId);
			}
		});

		that.getFdata(options);
		// || MP-WEIXIN || H5
		// #ifdef APP-PLUS
		// that.playVideo();
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5

		// #endif
	},
	onShow() {},
	onReady() {},
	components: {
		loading,
		cLabel,
		lsjUpload,
		fromItem
	},

	onUnload() {
		uni.$off('drawer-page')
	},

	methods: {
		// 切换表头
		yj_searchForm(num) {
			var that = this;
			this.Inv = num;
		},
		// 监理
		getJlusername(id) {
			this.JlusernameList = []; //监理用户列表初始值
			var getSupervisionUserList = []; //监理用户列表过滤后
			this.$http.getBaseDepartmentUser({
					fCompanyid: id
				})
				.then((res) => {
					if (res.errcode == 0) {
						res.data.childList.map((v) => {
							if (v.personList != undefined) {
								v.personList.map((item) => {
									this.JlusernameList.push(item);
								});
							}
						});

						this.JlusernameList.map((item) => {
							getSupervisionUserList.push(item.fRealname);
						});
						this.handle0 = getSupervisionUserList;
					} else {
						uni.showToast({
							title: "详情接口出错",
							icon: 'none',
							duration: 2000
						});
						return false;
					}
				})
				.catch((error) => {
					console.log("详情接口报错");
					return false;
				});

		},
		getFdata(data) {
			this.Fdata = JSON.parse(data.row);
			if (this.Fdata.fStatus == "待报验") {
				this.Inv = 0;
				this.readonly0 = false;
				this.readonly1 = true;
				this.must0 = "must";
				this.must1 = "must";
				this.closeNum = -1;
				this.FirstNum = -1;
			} else {
				this.Inv = 1;
				this.readonly0 = true;
				this.readonly1 = false;
				this.must0 = "no";
				this.must1 = "must";
				this.closeNum = 0;
				this.FirstNum = -1;
			}
			this.getDetailData(this.Fdata.fId, this.Inv);
		},
		getDetailData(id, num) {
			this.getDate();
			this.$http.getInspectionFormByFid({
					fId: id
				})
				.then((res) => {
					if (res.errcode == 0) {
						var that = this;
						that.totalData = res.data.inspectionform;
						uni.getStorage({
							key: 'userInfo',
							success: function(ress) {
								that.userInfo = JSON.parse(ress.data);
								if (num == 0) {
									//报验申请
									if (that.totalData.fInspectionnote == null) {
										that.totalData.fInspectionnote = ""
									}
									//技术负责人
									if (that.totalData.fDirectoruserid == null || that.totalData
										.fDirectorusername == null || that.totalData.fDirectoruserid ==
										"" || that.totalData.fDirectorusername == "") {
										that.totalData.fDirectoruserid = that.userInfo.userId;
										that.totalData.fDirectorusername = that.userInfo.realName;
									}
									//报验时间日期
									if (that.totalData.fInspectiondates == null || that.totalData
										.fInspectiondates == "") {
										that.totalData.fInspectiondates = that.timing;
									}
									//提请专监审核人
									if (that.totalData.fJlusername == null || that.totalData.fJluserid == null) {
										that.closeNum = -1;
										that.totalData.fJlusername = "";
										that.totalData.fJluserid = "";
									}else{
										that.closeNum = 0;
									}
								}

								if (num == 1) {
									//专监审核意见
									if (that.totalData.fJladuitnote == null) {
										that.totalData.fJladuitnote = ""
									}
									//专业监理工程师
									if (that.totalData.fJladuituserid == null || that.totalData
										.fJladuitusername == null || that.totalData
										.fJladuituserid == "" || that.totalData.fJladuitusername ==
										"") {
										that.totalData.fJladuituserid = that.userInfo.userId;
										that.totalData.fJladuitusername = that.userInfo.realName;
									}
									//审核日期
									if (that.totalData.fJladuitdates == null || that.totalData
										.fJladuitdates == "") {
										that.totalData.fJladuitdates = that.timing;
									}
								}

							}
						});




					} else {
						uni.showToast({
							title: "详情接口出错",
							icon: 'none',
							duration: 2000
						});
						return false;
					}
				})
				.catch((error) => {
					console.log("详情接口报错");
					return false;
				});


		},

		// 保存按钮
		clickNum(data) {
			this.saveData(data);
		},

		// 判断必填项
		saveData(num) {
			var that = this;
			if (num == 2) {
				if (
					that.totalData.fJladuitnote == "" ||
					that.totalData.fJladuitnote == undefined
				) {
					uni.showToast({
						title: '专监审核 专监审核意见不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (
					that.totalData.fJladuitusername == "" ||
					that.totalData.fJladuitusername == undefined
				) {
					uni.showToast({
						title: '专监审核 专业监理工程师不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (
					that.totalData.fJladuitdates == "" ||
					that.totalData.fJladuitdates == undefined
				) {
					uni.showToast({
						title: '专监审核 审核日期不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {
					that.clickN = 1;
					let totalData2 = {
						fId: this.totalData.fId, // 申请单fid //为空判断为新增
						fJladuitnote: this.totalData.fJladuitnote, //专监审核意见
						fJladuituserid: this.totalData.fJladuituserid, //专业监理工程师id
						fJladuitusername: this.totalData.fJladuitusername, //专业监理工程师
						fJladuitdates: this.totalData.fJladuitdates, //审核日期
						userId: this.userInfo.userId, // 当前登录人id
						userName: this.userInfo.realName, // 当前登录人姓名
					};
					this.$http.saveJlInspectionForm(totalData2)
						.then((res) => {
							if (res.errcode == 0) {
								that.$refs.loading.open();
								that.page = false
								if (!that.click) {
									setTimeout(() => {
										uni.showToast({
											title: "成功",
											icon: 'none',
											duration: 2000
										});
										uni.redirectTo({
											url: "/pages/apply/testDetectionManage/InspectionForm/testing/index?Inv=" +
												0,
											success: () => {
												that.clickN = 1
												that.click = false
												that.$refs.loading.close();
												// #ifdef H5
												setTimeout(() => {
													that.click = false;
												}, 100)
												// #endif

											}

										})
									}, 1500)
								}
								that.click = true
							} else {
								uni.showToast({
									title: "保存失败",
									icon: 'none',
									duration: 2000
								});
								that.clickN = 0;
								this.$refs.loading.close();
							}
						})
				}



			} else {
				if (
					that.totalData.fInspectionnote == "" ||
					that.totalData.fInspectionnote == undefined
				) {
					uni.showToast({
						title: '报验信息 报验申请不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (
					that.totalData.fDirectorusername == "" ||
					that.totalData.fDirectorusername == undefined
				) {
					uni.showToast({
						title: '报验信息 技术负责人不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (
					that.totalData.fInspectiondates == "" ||
					that.totalData.fInspectiondates == undefined
				) {
					uni.showToast({
						title: '报验信息 报验时间日期不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (
					that.totalData.fJlusername == "" ||
					that.totalData.fJlusername == undefined
				) {
					uni.showToast({
						title: '报验信息 提请专监审核人不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {
					that.clickN = 1;
					var isSubmit = "";
					if (num == 0) {
						isSubmit = "0";
					} else if (num == 1) {
						isSubmit = "1";
					}
					let totalData1 = {
						fId: this.totalData.fId, // 申请单fid //为空判断为新增
						fInspectionnote: this.totalData.fInspectionnote, //报验申请
						fDirectoruserid: this.totalData.fDirectoruserid, //技术负责人id
						fDirectorusername: this.totalData.fDirectorusername, //技术负责人
						fInspectiondates: this.totalData.fInspectiondates, //报验时间日期*
						fJluserid: this.totalData.fJluserid, //提请专监审核人id
						fJlusername: this.totalData.fJlusername, //提请专监审核人
						userId: this.userInfo.userId, // 当前登录人id
						userName: this.userInfo.realName, // 当前登录人姓名
						isSubmit: isSubmit,
					};
					this.$http.saveInspectionForm(totalData1)
						.then((res) => {
							if (res.errcode == 0) {
								that.$refs.loading.open();
								that.page = false
								if (!that.click) {
									setTimeout(() => {
										uni.showToast({
											title: "成功",
											icon: 'none',
											duration: 2000
										});
										uni.redirectTo({
											url: "/pages/apply/testDetectionManage/InspectionForm/testing/index?Inv=" +
												0,
											success: () => {
												that.clickN = 1
												that.click = false
												that.$refs.loading.close();
												// #ifdef H5
												setTimeout(() => {
													that.click = false;
												}, 100)
												// #endif

											}
										})
									}, 1500)
								}
								that.click = true
							} else {
								uni.showToast({
									title: "保存失败",
									icon: 'none',
									duration: 2000
								});
								that.clickN = 0;
								this.$refs.loading.close();
							}
						})
				}
			}
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			//   const hh = (time.getHours() + "").padStart(2, "0");
			//   const mm = (time.getMinutes() + "").padStart(2, "0");
			//   const ss = (time.getSeconds() + "").padStart(2, "0");

			//   var timer = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
			this.timing = `${y}-${m}-${d}`;
		},
		selectorFun(e) {
			// 提请专监审核人
			this.JlusernameList.map(item => {
				if (e == item.fRealname) {
					this.totalData.fJlusername = item.fRealname;
					this.totalData.fJluserid = item.fUserid;
				}
			})
		},

		// 原生窗体按钮显示
		// playVideo() {
		// 	let subNVue = uni.getSubNVueById('safe_notice')
		// 	subNVue.show()
		// },
		callback() {},

	}
}
